home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / bmscaler / bmscaler.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-20  |  6.9 KB  |  236 lines

  1. // BMSCALER.CPP
  2. //
  3. // last modified: May 20, 1994, 8:14pm
  4. //
  5. // coded by Tumblin / Bodies In Motion
  6. //
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12. #include "xlib_all.h"
  13.  
  14. #include "bitmap.h"
  15. #include "palette.h"
  16.  
  17. #define SOURCE_WIDTH 79
  18. #define SOURCE_HEIGHT 69
  19. #define CNTR_X 160
  20. #define CNTR_Y 120
  21.  
  22.  
  23. // Oh, set the tabs in your editor to 2 so it will look like mine.
  24.  
  25. //=================== Stuff for this demo program ========================
  26.  
  27. // function prototypes
  28. int main(void);
  29. void bmscaler(int top_left_x, int top_left_y,
  30.                             int bottom_right_x, int bottom_right_y,
  31.                             int source_width, int source_height,
  32.                             int page_offset, char * bitmap);
  33.  
  34. // main program
  35. int main(void)
  36. {
  37.     int count;
  38.     x_text_mode();
  39.     printf("\n\n\n\n\n\n\n");
  40.     printf("                              BITMAP SCALING\n\n");
  41.     printf("                       by Tumblin / Bodies In Motion\n\n\n");
  42.     printf("          Press any key to see a scaling bitmap of your's truly :-)\n");
  43.     printf("                        Press any key again to end.\n\n");
  44.     getch();
  45.     x_set_mode(X_MODE_320x240,320);
  46.     x_set_doublebuffer(240);
  47.     x_put_pal_raw(palette,64,0);
  48.     do
  49.     {
  50.         for(count=0;count <50; count++)
  51.         {
  52.             x_rect_fill(CNTR_X-51,CNTR_Y-51,CNTR_X+51,CNTR_Y+51,HiddenPageOffs,0);
  53.             bmscaler(CNTR_X-count,CNTR_Y-count,CNTR_X+count,CNTR_Y+count,
  54.                              SOURCE_WIDTH,SOURCE_HEIGHT,HiddenPageOffs,bitmap);
  55.             x_page_flip(0,0);
  56.             if(kbhit()) break;
  57.         }
  58.         for(count=50;count >=0; count--)
  59.         {
  60.             x_rect_fill(CNTR_X-51,CNTR_Y-51,CNTR_X+51,CNTR_Y+51,HiddenPageOffs,0);
  61.             bmscaler(CNTR_X-count,CNTR_Y-count,CNTR_X+count,CNTR_Y+count,
  62.                              SOURCE_WIDTH,SOURCE_HEIGHT,HiddenPageOffs,bitmap);
  63.             x_page_flip(0,0);
  64.             if(kbhit()) break;
  65.         }
  66.     } while(!kbhit());
  67.     getch();
  68.     x_text_mode();
  69.     printf("Thank you for taking a look at this source code.\n\n");
  70.     printf("Here's how you can contact me:\n\n");
  71.     printf("mail:     Terry Sznober (a.k.a. Tumblin / Bodies In Motion)\n");
  72.     printf("          193 Churchill Blvd. Apt.4\n");
  73.     printf("          Saint John, New Brunswick\n");
  74.     printf("          Canada\n");
  75.     printf("          E2K 3E2\n\n");
  76.     printf("Internet: m9cl@acad1.unbsj.ca\n");
  77.     printf("          m9cl@unbsj.ca\n");
  78.     printf("          m9cl@unb.ca\n\n");
  79.     printf("Phone:    (506) 652-3516\n\n");
  80.     printf("Greetings go to:\n");
  81.     printf("Psi Gore Pixel Skaven & Purple Motion / Future Crew\n");
  82.     printf("Fear / Mental Design, Pelusa & Necros / Psychic Monks\n");
  83.     printf("Lord Logics, Vogue Mr.H & Lizardking / Trition,\n");
  84.     printf("Themie Gouthas, Chris & Barry Egerter, and Ronski.\n");
  85.     return(0);
  86. }
  87.  
  88.  
  89. //======================== Bitmap Scaling Code =============================
  90.  
  91. void bmscaler(int top_left_x, int top_left_y,
  92.                             int bottom_right_x, int bottom_right_y,
  93.                             int source_width, int source_height,
  94.                             int page_offset, char * bitmap)
  95. {
  96.     // declare local variables
  97.     int color,error_term_x,error_term_y,source_x,source_y,screen_x,screen_y;
  98.     int destination_width,destination_height;
  99.  
  100.     // initialize local variables
  101.     source_x=0;
  102.     source_y=0;
  103.     error_term_x=0;
  104.     error_term_y=0;
  105.     screen_x=top_left_x;    // starting x coordinate to draw
  106.     screen_y=top_left_y;    // starting y coordinate to draw
  107.     destination_width=bottom_right_x-top_left_x;
  108.     destination_height=bottom_right_y-top_left_y;
  109.  
  110.     // here we go, into the scaling stuff.  Pick which case we have and do it
  111.  
  112.     //---------------------------------------------------------
  113.     // case #1: fatter and taller destination bitmap
  114.     if((destination_width  > source_width) &&
  115.          (destination_height > source_height))
  116.     {
  117.         do    // destination fatter (x-axis) than source bitmap
  118.         {
  119.             error_term_x+=source_width;
  120.             if(error_term_x > destination_width)
  121.             {
  122.                 error_term_x-=destination_width;
  123.                 source_x++;
  124.             }
  125.             screen_x++;
  126.             screen_y=top_left_y;
  127.             source_y=0;
  128.             do    // destination taller (y-axis) than source bitmap
  129.             {
  130.                 color=bitmap[source_y*source_width+source_x];
  131.                 x_put_pix(screen_x,screen_y,page_offset,color);
  132.                 error_term_y+=source_height;
  133.                 if(error_term_y > destination_height)
  134.                 {
  135.                     error_term_y-=destination_height;
  136.                     source_y++;
  137.                 }
  138.                 screen_y++;
  139.             } while(screen_y < (bottom_right_y));
  140.         } while(screen_x < (bottom_right_x));
  141.     }
  142.     else
  143.     //---------------------------------------------------------
  144.     // case #2: fatter and shorter destination bitmap
  145.     if((destination_width > source_width) &&
  146.          (destination_height < source_height))
  147.     {
  148.         do    // destination fatter (x-axis) than source bitmap
  149.         {
  150.             error_term_x+=source_width;
  151.             if(error_term_x > destination_width)
  152.             {
  153.                 error_term_x-=destination_width;
  154.                 source_x++;
  155.             }
  156.             screen_x++;
  157.             screen_y=top_left_y;
  158.             source_y=0;
  159.             do    // destination shorter (y-axis) than source bitmap
  160.             {
  161.                 color=bitmap[source_y*source_width+source_x];
  162.                 x_put_pix(screen_x,screen_y,page_offset,color);
  163.                 error_term_y+=destination_height;
  164.                 if(error_term_y > source_height)
  165.                 {
  166.                     error_term_y-=source_height;
  167.                     screen_y++;
  168.                 }
  169.                 source_y++;
  170.             } while(screen_y < (bottom_right_y));
  171.         } while(screen_x < (bottom_right_x));
  172.     }
  173.     //---------------------------------------------------------
  174.     // case #3: skinnier and taller destination bitmap
  175.     if((destination_width < source_width) &&
  176.          (destination_height > source_height))
  177.     {
  178.         do    // destination skinnier (x-axis) than source bitmap
  179.         {
  180.             error_term_x+=destination_width;
  181.             if(error_term_x > source_width)
  182.             {
  183.                 error_term_x-=source_width;
  184.                 screen_x++;
  185.             }
  186.             source_x++;
  187.             screen_y=top_left_y;
  188.             source_y=0;
  189.             do    // destination taller (y-axis) than source bitmap
  190.             {
  191.                 color=bitmap[source_y*source_width+source_x];
  192.                 x_put_pix(screen_x,screen_y,page_offset,color);
  193.                 error_term_y+=source_height;
  194.                 if(error_term_y > destination_height)
  195.                 {
  196.                     error_term_y-=destination_height;
  197.                     source_y++;
  198.                 }
  199.                 screen_y++;
  200.             } while(screen_y < (bottom_right_y));
  201.         } while(screen_x < (bottom_right_x-2));
  202.     }
  203.     //---------------------------------------------------------
  204.     // case#4: skinnier and shorter destination bitmap
  205.     if((destination_width <= source_width) &&
  206.          (destination_height <= source_height))
  207.     {
  208.         do    // destination skinnier (x-axis) than source bitmap
  209.         {
  210.             error_term_x+=destination_width;
  211.             if(error_term_x > source_width)
  212.             {
  213.                 error_term_x-=source_width;
  214.                 screen_x++;
  215.             }
  216.             source_x++;
  217.             screen_y=top_left_y;
  218.             source_y=0;
  219.             do    // destination shorter (y-axis) than source bitmap
  220.             {
  221.                 color=bitmap[source_y*source_width+source_x];
  222.                 x_put_pix(screen_x,screen_y,page_offset,color);
  223.                 error_term_y+=destination_height;
  224.                 if(error_term_y > source_height)
  225.                 {
  226.                     error_term_y-=source_height;
  227.                     screen_y++;
  228.                 }
  229.                 source_y++;
  230.             } while(screen_y < (bottom_right_y));
  231.         } while(screen_x < (bottom_right_x-2));
  232.     }
  233.     //---------------------------------------------------------
  234.     // end of the four cases
  235. }
  236.